﻿using Common.Contract.Authentication.Record;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.CustomerManager;
using Common.Contract.Customer.Response.CustomerManager;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Customer.Core.ServiceCommand.CustomerManager
{
    public class RetrieveCustomerCardDetailByCifServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveCustomerCardDetailByCifRequest, RetrieveCustomerCardDetailByCifResponse>
    {
        #region Properties
        public UserRecord Dao;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public FilterRecord SearchColumns = null;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveCustomerCardDetailByCifServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveCustomerCardDetailByCifRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
            SearchColumns = Request.Filter;
        }

        protected override bool OnExecute(RetrieveCustomerCardDetailByCifResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<CustomerCardDetailRecord> records = new List<CustomerCardDetailRecord>();
                List<ProductCardRawDaily> listCustomer = new List<ProductCardRawDaily>();
                listCustomer = Providers.Customers.RetrieveCustomerCardByCif(out totalRecord, Dao, SearchColumns, pageSize, pageIndex);
                if (listCustomer != null)
                {
                    records = listCustomer.Select(x => new CustomerCardDetailRecord
                    {
                        Cif = x.Cif,
                        ContractNumber = x.ContractNumber,
                        ContractStatus = x.ContractStatus,
                        ProductionStatus = x.ProductionStatus,
                        CardStatus = x.CardStatus,
                        CardNumber = x.CardNumber,
                        ScoredCard = x.ScoredCard,
                        Balance = x.Balance,
                        BranchId = x.BranchId,
                        CusName = x.CusName,
                        Dao = x.Dao,
                        UnlockCard = x.UnlockCard,
                        UnlockDate = x.UnlockDate,
                        TypeCard = x.TypeCard,
                        CardLimit = x.CardLimit
                    }).ToList();
                }
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;
            }
            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
